package com.chinacoal.hr.levelimpl.controller;

import com.chinacoal.hr.levelapi.client.LoginClient;
import com.chinacoal.hr.levelapi.client.response.Result;
import com.chinacoal.hr.levelapi.client.response.ResultGenerator;
import com.chinacoal.hr.levelimpl.client.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author: YunTao.Li
 * @create: 2019-12-11 14:26
 * @description:
 **/
@Controller
public class LoginController implements LoginClient{

    @Autowired
    private LoginUser loginUser;

    @Autowired
    private HttpServletRequest request;

    public Map login() {
        Map map = new HashMap();
        map.put("code", 200);

        Map data = new HashMap();
        String[] roles = {"admin"};
        data.put("name", "Super Admin");
        data.put("roles", roles);
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

        map.put("data", data);

        return map;
    }

    public Map userinfo() {
        Map map = new HashMap();
        map.put("code", 200);
        Map data = new HashMap();
        String[] roles = {"admin"};
        data.put("roles", roles);
        data.put("name", "Super Admin");
        data.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");

        map.put("data", data);
        return map;
    }

    /**
     * 获得登陆的用户
     *
     * @return : Result
     * @author : YunTao.Li
     * @date : 2019/12/11 2019/12/11
     */
    public Result user(@RequestHeader HttpHeaders headers) {
        Map userMap = new HashMap();
//        userMap.put("empId", "10011762");
//        userMap.put("empCode", "10011762");
//        userMap.put("empName", "刘爽");
//        userMap.put("deptCode", "KF47055");
//        userMap.put("deptName", "业务二处");

        try {
            Map<String, Object> mapUser = loginUser.getLoginUser();

            userMap.put("empCode", mapUser.get("empCode"));
            userMap.put("empName", mapUser.get("empName"));
            userMap.put("deptCode", mapUser.get("deptCode"));
            userMap.put("deptName", mapUser.get("deptName"));
            userMap.put("orgCode", mapUser.get("orgCode"));
            userMap.put("orgName", mapUser.get("orgName"));
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultGenerator.genFailedResult(e.getMessage());
        }

        return ResultGenerator.genOkResult(userMap);
    }
}
